package org.cjprods.rsms;

import javax.json.Json;
import javax.json.JsonValue;
import org.cjprods.rsms.annotations.*;

public class services
{
    // SPECIAL
    public static JsonValue no_such_api()
    {
        throw new utils.failure("no such api");
    }

// <editor-fold desc="user">
    @path("/user/login")
    @sql(func="user_info",
         values={"username"},
         results={"seq", "credential", "role", "name"})
    public static JsonValue user_login(
            @arg("password") String password,
            @result("credential") String credential,
            @result("seq") String seq,
            @result("role") String role,
            @result("name") String name)
    {
        if (!utils.check_password(credential, password))
            throw new utils.failure("auth failed");

        String token = utils.make_token(seq, role);
        return Json.createObjectBuilder()
                .add("token", token)
                .add("name", name)
                .build();
    }

    @path("/user/validate")
    @token
    public static void user_validate() {}

    @path("/user/add")
    @post
    @token(roles={"信息部管理员"})
    @sql(func="add_user",
         values={"username", "*password", "role", "name"})
    public static void user_add() {}

    @path("/user/list")
    @token(roles={"信息部管理员"})
    @sql(func="list_users",
         values={"after"},
         results={"id", "username", "role", "name"},
         multi=true)
    public static void user_list() {}

    @path("/user/password")
    @token(roles={"信息部管理员"})
    @sql(func="modify_password",
         values={"user", "*new"})
    public static void user_password() {}

    @path("/user/password/self")
    @token
    @sql(func="modify_password",
         values={"#", "*new"})
    public static void user_password_self() {}

    @path("/user/name")
    @token(roles={"信息部管理员"})
    @sql(func="modify_name",
         values={"user", "new"})
    public static void user_name() {}

    @path("/user/role")
    @token(roles={"信息部管理员"})
    @sql(func="modify_role",
         values={"user", "new"})
    public static void user_role() {}

    @path("/user/revoke/self")
    @token
    @sql(func="revoke_user",
         values={"#"})
    public static void user_revoke_self() {}

    @path("/user/leave")
    @token(roles={"信息部管理员"})
    @sql(func="leave_user",
         values={"user"})
    public static void user_leave() {}
// </editor-fold>

// <editor-fold desc="sale">
    @path("/sale/add")
    @post
    @token(roles={"销售部业务员"})
    @sql(func="add_sale",
         values={"#", "purpose", "budget"})
    public static void sale_add() {}

    @path("/sale/cancel")
    @token(roles={"销售部业务员"})
    @sql(func="cancel_sale",
         values={"#", "sale"})
    public static void sale_cancel() {}

    @path("/sale/approve")
    @token(roles={"销售部经理"})
    @sql(func="approve_sale",
         values={"sale"})
    public static void sale_approve() {}

    @path("/sale/reject")
    @post
    @token(roles={"销售部经理"})
    @sql(func="reject_sale",
         values={"sale", "reason"})
    public static void sale_reject() {}

    @path("/sale/list")
    @token(roles={"销售部经理"})
    @sql(func="list_sales",
         values={"after"},
         results={"id", "applicant", "approval", "purpose", "budget", "date"},
         multi=true)
    public static void sale_list() {}

    @path("/sale/list/self")
    @token(roles={"销售部业务员"})
    @sql(func="list_my_sales",
         values={"after", "#"},
         results={"id", "approval", "purpose", "budget", "date"},
         multi=true)
    public static void sale_list_self() {}

    @path("/sale/activity/add")
    @post
    @token(roles={"销售部业务员"})
    @sql(func="add_activity",
         values={"#", "sale", "activity", "cost"})
    public static void sale_activity_add() {}

    @path("/sale/activity/list")
    @token(roles={"销售部经理"})
    @sql(func="list_activitys",
         values={"after", "sale"},
         results={"id", "activity", "cost", "date"},
         multi=true)
    public static void sale_activity_list() {}

    @path("/sale/activity/list/self")
    @token(roles={"销售部业务员"})
    @sql(func="list_my_activitys",
         values={"after", "#", "sale"},
         results={"id", "activity", "cost", "date"},
         multi=true)
    public static void sale_activity_list_self() {}
// </editor-fold>

// <editor-fold desc="ticket">
    @path("/ticket/add")
    @post
    @token(roles={"售后部信息员"})
    @sql(func="add_ticket",
         values={"complaint", "customer", "contact"})
    public static void ticket_add() {}

    @path("/ticket/assign")
    @token(roles={"售后部信息员"})
    @sql(func="assign_ticket",
         values={"ticket", "user"})
    public static void ticket_assign() {}

    @path("/ticket/list")
    @token(roles={"售后部经理"})
    @sql(func="list_tickets",
         values={"after"},
         results={"id", "complaint", "customer", "assignee", "result", "detail"},
         multi=true)
    public static void ticket_list() {}

    @path("/ticket/list/self")
    @token(roles={"售后部业务员"})
    @sql(func="list_my_tickets",
         values={"after", "#"},
         results={"id", "complaint", "customer", "contact", "result", "detail"},
         multi=true)
    public static void ticket_list_self() {}

    @path("/ticket/list/latest")
    @token(roles={"售后部信息员"})
    @sql(func="list_latest_tickets",
         values={"after"},
         results={"id", "complaint", "customer", "result", "detail"},
         multi=true)
    public static void ticket_list_latest() {}

    @path("/ticket/list/free")
    @token(roles={"售后部信息员"})
    @sql(func="list_free_assignees",
         values={"after"},
         results={"id", "name"},
         multi=true)
    public static void ticket_list_free() {}

    @path("/ticket/done")
    @token(roles={"售后部业务员"})
    @sql(func="done_assignment",
         values={"assignment", "#"})
    public static void ticket_done() {}

    @path("/ticket/fail")
    @post
    @token(roles={"售后部业务员"})
    @sql(func="fail_assignment",
         values={"assignment", "#", "detail"})
    public static void ticket_fail() {}

    @path("/ticket/waive")
    @post
    @token(roles={"售后部业务员"})
    @sql(func="waive_assignment",
         values={"assignment", "#", "detail"})
    public static void ticket_waive() {}
// </editor-fold>
}

